<template>
    <div class="app-container">
        <div class="p4">
            <FaSearchBar :show-toggle="false">
                <template #default="{ fold, toggle }">
                    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="100px">
                        <el-form-item label="应用名称" prop="appName">
                            <el-input v-model="queryParams.appName" placeholder="请输入应用名称" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="版本号" prop="version">
                            <el-input v-model="queryParams.version" placeholder="请输入版本号" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="平台" prop="platform" v-show="!fold">
                            <el-input v-model="queryParams.platform" placeholder="请输入平台" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="包名" prop="packageName" v-show="!fold">
                            <el-input v-model="queryParams.packageName" placeholder="请输入包名" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                            <el-button @click="resetQuery">重置</el-button>
                            <el-button link @click="toggle">
                                <template #icon>
                                    <FaIcon :name="fold ? 'i-ep:caret-bottom' : 'i-ep:caret-top'" />
                                </template>
                                {{ fold ? '高级查询' : '高级查询' }}
                            </el-button>
                        </el-form-item>
                    </el-form>
                </template>
            </FaSearchBar>
            <el-divider border-style="dashed" style="margin:0px 0px 20px 0px" />

            <el-space wrap>
                <el-button type="primary" icon="Plus" @click="handleAdd" v-auth="['app:version$add']">新增
                </el-button>
                <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate"
                    v-auth="['app:version$edit']">修改
                </el-button>
                <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-auth="['app:version$remove']">删除
                </el-button>
                <el-button type="warning" icon="Download" @click="handleExport" v-auth="['app:version$export']">导出
                </el-button>
            </el-space>

            <el-table v-loading="loading" :data="versionList" @selection-change="handleSelectionChange" stripe
                highlight-current-row border class="mt4">
                <el-table-column type="selection" width="55" align="center" />
                <!-- <el-table-column label="编号" align="center" prop="id" /> -->
                <el-table-column label="平台" align="center" prop="platform" width="100" />
                <el-table-column label="应用名称" align="left" prop="appName" />
                <el-table-column label="版本号" align="left" prop="version" width="80" />
                <el-table-column label="包名" align="left" prop="packageName" width="150" />
                <el-table-column label="应用地址" align="left" prop="url" show-overflow-tooltip />
                <!-- <el-table-column label="大小" align="left" prop="size" width="80" /> -->
                <el-table-column label="版本后缀" align="center" prop="suffix" width="100" />
                <el-table-column label="强制更新" align="center" prop="coerce" width="100">
                    <template #default="scope">
                        <DictTag :dictData="sys_is" :value="scope.row.coerce" />
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="status" width="60">
                    <template #default="scope">
                        <DictTag :dictData="sys_common_status" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <!-- <el-table-column label="更新日志" align="center" prop="remarks" /> -->
                <!-- <el-table-column label="创建人" align="left" prop="createdByDesc" width="100" /> -->
                <el-table-column label="创建时间" align="center" prop="updatedAt" width="180" />
                <el-table-column label="操作" fixed="right" align="left" width="200">
                    <template #default="scope">
                        <el-button size="small" plain type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                            v-auth="['app:version$edit']">修改
                        </el-button>
                        <el-button size="small" plain type="danger" icon="Delete" @click="handleDelete(scope.row)"
                            v-auth="['app:version$remove']">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </div>
        <!-- 添加或修改应用版本对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="versionRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="平台" label-width="100" prop="platform">
                    <el-select v-model="form.platform" clearable placeholder="请选择平台">
                        <el-option v-for="dict in sys_client_type" :key="dict.value" :label="dict.label"
                            :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="应用名称" label-width="100" prop="appName">
                    <el-input v-model="form.appName" placeholder="请输入应用名称" />
                </el-form-item>
                <el-form-item label="版本号" label-width="100" prop="version">
                    <el-input v-model="form.version" placeholder="请输入版本号" />
                </el-form-item>
                <el-form-item label="包名" label-width="100" prop="packageName">
                    <el-input v-model="form.packageName" placeholder="请输入包名" />
                </el-form-item>
                <el-form-item label="应用地址" label-width="100" prop="url">
                    <BigFileUpload :fileValue="form.url" :uploadSuccessContinue="false" @removeSuccess="removeSuccess"
                        @uploadSuccess="uploadSuccess"></BigFileUpload>
                </el-form-item>
                <el-form-item label="强制更新" label-width="100" prop="coerce">
                    <el-radio-group v-model="form.coerce">
                        <el-radio v-for="dict in sys_is" :key="dict.value" :label="dict.value">{{ dict.label }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="更新日志" label-width="100" prop="remarks">
                    <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="状态" label-width="100" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio v-for="dict in sys_common_status" :key="dict.value" :label="dict.value">{{ dict.label
                        }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Version">
import apiVersion from "@/api/system/version";

const { proxy } = getCurrentInstance();

const { sys_common_status, sys_client_type, sys_is } = proxy.useDict("sys_common_status", "sys_client_type", "sys_is");
const versionList = ref([]);
const open = ref(false);
const loading = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        platform: null,
        appName: null,
        version: null,
        packageName: null,
        url: null,
        size: null,
        suffix: null,
        coerce: null,
        status: null,
        remarks: null,
    },
    rules: {
        platform: [
            { required: true, message: "平台不允许为空", trigger: "blur" }
        ],
        appName: [
            { required: true, message: "应用名称不允许为空", trigger: "blur" }
        ],
        version: [
            { required: true, message: "版本号不允许为空", trigger: "blur" }
        ],
        packageName: [
            { required: true, message: "包名不允许为空", trigger: "blur" }
        ],
        url: [
            { required: true, message: "应用地址不允许为空", trigger: "blur" }
        ]
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询应用版本列表 */
function getList() {
    // loading.value = true;
    apiVersion.page(queryParams.value).then(response => {
        versionList.value = response.data.rows;
        total.value = response.data.total;
    }).finally(() => {
        loading.value = false
    })
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}


// 表单重置
function reset() {
    form.value = {
        id: null,
        platform: null,
        appName: null,
        version: null,
        packageName: null,
        url: null,
        size: null,
        suffix: null,
        coerce: "1",
        status: "0",
        remarks: null
    };
    proxy.resetForm("versionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加应用版本";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    apiVersion.getById(_id).then(response => {
        response.data.coerce = response.data.coerce + "";
        response.data.status = response.data.status + "";
        form.value = response.data;
        open.value = true;
        title.value = "修改应用版本";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["versionRef"].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                apiVersion.update(form.value).then(response => {
                    proxy.$modal.success("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                apiVersion.add(form.value).then(response => {
                    proxy.$modal.success("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除应用版本编号为"' + _ids + '"的数据项？').then(function () {
        return apiVersion.delete(_ids);
    }).then(() => {
        getList();
        proxy.$modal.success("删除成功");
    })
}

/** 导出按钮操作 */
function handleExport() {
    apiVersion.download(queryParams.value, "应用版本列表");
}

function removeSuccess(url) {
    form.value.url = url;
}

function uploadSuccess(url) {
    if (form.value.url == null || form.value.url.length == 0) {
        form.value.url = url
    } else {
        form.value.url += "," + url
    }
}

getList();
</script>
